<?php

namespace App\Http\Controllers\Entity\Asset\Vlive;

use App\Http\Controllers\Controller;
use App\Http\Requests\Entity\Asset\Vlive\VcposterRequest;
use App\Models\Entity\Asset\Vlive\Vcposter;
use App\Models\Entity\Asset\Vlive\Vchan;
use App\Repositories\Entity\Asset\Vlive\VcposterRepository;
use Encore\Admin\Controllers\ModelForm;
use App\Models\Common\CatData\Image\Imgtpl;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Flash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Route;
use Response;


class VcposterController extends Controller
{
    use ModelForm;

    /** @var  VcposterRepository */
    private $vcposterRepository;

    public function __construct(VcposterRepository $vcposterRepo)
    {
        $this->vcposterRepository = $vcposterRepo;
    }

    /**
     * Display a listing of the Vcposter.
     *
     * @return Response
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(__('entity.asset.vlive.vcposters.index_header'));
            $content->description(__('entity.asset.vlive.vcposters.index_description'));

            $content->body($this->grid());
        });
    }

    /**
     * Show the form for creating a new Vcposter.
     *
     * @return Response
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(__('entity.asset.vlive.vcposters.create_header'));
            $content->description(__('entity.asset.vlive.vcposters.create_description'));

            $content->body($this->form());

        });
    }

    /**
     * Store a newly created Vcposter in storage.
     *
     * @param VcposterRequest $request
     *
     * @return Response
     */
    public function store(VcposterRequest $request)
    {
        $input = $request->all();

        

        $vcposter = $this->vcposterRepository->create($input);


        Flash::success(__('entity.asset.vlive.vcposters.saved_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.vlive.vcposters.index'));

    }

    /**
     * Display the specified Vcposter.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $vcposter = $this->vcposterRepository->findWithoutFail($id);

        if (empty($vcposter)) {
            Flash::error(__('entity.asset.vlive.vcposters.not_found'));

            return redirect(route('entity.asset.vlive.vcposters.index'));
        }

        return view('entity.asset.vlive.vcposters.show')->with('vcposter', $vcposter);

    }

    /**
     * Show the form for editing the specified Vcposter.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {


        return Admin::content(function (Content $content) use ($id) {

            $content->header(__('entity.asset.vlive.vcposters.edit_header'));
            $content->description(__('entity.asset.vlive.vcposters.edit_description'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Update the specified Vcposter in storage.
     *
     * @param  int              $id
     * @param VcposterRequest $request
     *
     * @return Response
     */
    public function update($id, VcposterRequest $request)
    {
        $vcposter = $this->vcposterRepository->findWithoutFail($id);

        if (empty($vcposter)) {
            Flash::error(__('entity.asset.vlive.vcposters.not_found'));

            return redirect(route('entity.asset.vlive.vcposters.index'));
        }
        $input = $request->all();

        $vcposter = $this->vcposterRepository->update($input, $id);


        Flash::success(__('entity.asset.vlive.vcposters.updated_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.vlive.vcposters.index'));
    }

    /**
     * Remove the specified Vcposter from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        // 根据 `,` 判断传递过来的是单个id还是多个id
        if (substr_count($id, ',') >= 1) {
            $id = explode(",", $id);
        }

        // 如果是数组则进行批量删除
        if (is_array($id)) {
            if ($flag = $this->vcposterRepository->batchDelete('id', $id)) {

                return response()->json(['message' => __('entity.asset.vlive.vcposters.deleted_success'), 'status' => $flag]);
            } else {
                return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
            }
        }

        $vcposter = $this->vcposterRepository->findWithoutFail($id);

        if (empty($vcposter)) {
            Flash::error(__('entity.asset.vlive.vcposters.not_found'));

            return redirect(route('entity.asset.vlive.vcposters.index'));
        }

        if ($flag = $this->vcposterRepository->delete($id)) {


            return response()->json(['message' => __('entity.asset.vlive.vcposters.deleted_success'), 'status' => $flag]);
        } else {
            return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
        }
    }

    /**
     * 直播的海报crud
     */
    /**
     * Display a listing of the vcposter.
     *
     * @return Response
     */
    public function vchanIndex($vchan_id)
    {
        return Admin::content(function (Content $content) {

            $content->header(__('entity.asset.vlive.vcposters.index_header'));
            $content->description(__('entity.asset.vlive.vcposters.index_description'));

            $content->body($this->grid());
        });
    }

    /**
     * 直播的海报新建
     *
     * @return Response
     */
    public function vchanCreate($vchan_id)
    {
        // 如果媒资已经上线且发布,则不让新增、编辑、删除操作
        $data = Vchan::where('id', '=', $vchan_id)->select('online_status', 'publish_status')->first();

        if ($data && $data->online_status == 1 && $data->publish_status == 1) {

            Flash::error(('该直播已上线发布,不能新增海报'));

            return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.vlive.vchan_details', ['vchan_id' => $vchan_id]));
        }

        return Admin::content(function (Content $content) {

            $content->header(__('entity.asset.vlive.vcposters.create_header'));
            $content->description(__('entity.asset.vlive.vcposters.create_description'));

            $content->body($this->form());

        });
    }

    /**
     * Store a newly created Vposteri in storage.
     *
     * @param VposteriRequest $request
     *
     * @return Response
     */
    public function vchanStore($vchan_id, VcposterRequest $request)
    {
        // 如果媒资已经上线且发布,则不让新增、编辑、删除操作
        $data = Vchan::where('id', '=', $vchan_id)->select('online_status', 'publish_status')->first();

        if ($data && $data->online_status == 1 && $data->publish_status == 1) {

            Flash::error(('该直播已上线发布,不能新增海报'));

            return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.vlive.vchan_details', ['vchan_id' => $vchan_id]));
        }

        // 新建的时候和剧集关联
        if (!$vchan = Vchan::find($vchan_id)) {
            Flash::error('直播不能为空!');
            return redirect(route('entity.asset.vlive.vcposters.index'));
        }

        $input    = $request->all();
        $vcposter = $this->vcposterRepository->create($input);

        $vchan->vcposters()->save($vcposter);

        Flash::success(__('entity.asset.vlive.vcposters.saved_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.vlive.vchan_details', ['vchan_id' => $vchan_id]));

    }

    /**
     * Show the form for editing the specified Vposteri.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function vchanEdit($vchan_id, $id)
    {
        // 如果媒资已经上线且发布,则不让新增、编辑、删除操作
        $data = Vchan::where('id', '=', $vchan_id)->select('online_status', 'publish_status')->first();

        if ($data && $data->online_status == 1 && $data->publish_status == 1) {

            Flash::error(('该直播已上线发布,不能修改海报'));

            return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.vlive.vchan_details', ['vchan_id' => $vchan_id]));
        }

        return Admin::content(function (Content $content) use ($id) {

            $content->header(__('entity.asset.vlive.vcposters.edit_header'));
            $content->description(__('entity.asset.vlive.vcposters.edit_description'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Update the specified Vposteri in storage.
     *
     * @param  int              $id
     * @param VposteriRequest $request
     *
     * @return Response
     */
    public function vchanUpdate($vchan_id, $id, VcposterRequest $request)
    {
        // 如果媒资已经上线且发布,则不让新增、编辑、删除操作
        $data = Vchan::where('id', '=', $vchan_id)->select('online_status', 'publish_status')->first();

        if ($data && $data->online_status == 1 && $data->publish_status == 1) {

            Flash::error(('该直播已上线发布,不能修改海报'));

            return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.vlive.vchan_details', ['vchan_id' => $vchan_id]));
        }

                $vcposter = $this->vcposterRepository->findWithoutFail($id);

        if (empty($vcposter)) {
            Flash::error(__('entity.asset.vlive.vcposters.not_found'));

            return redirect(route('entity.asset.vlive.vcposters.index'));
        }
        $input = $request->all();

        $vcposter = $this->vcposterRepository->update($input, $id);

        Flash::success(__('entity.asset.vlive.vcposters.updated_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('entity.asset.vlive.vchan_details', ['vchan_id' => $vchan_id]));
    }

    /**
     * Remove the specified Vposteri from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function vchanDestroy($vchan_id, $id)
    {
        // 如果媒资已经上线且发布,则不让新增、编辑、删除操作
        $data = Vchan::where('id', '=', $vchan_id)->select('online_status', 'publish_status')->first();

        if ($data && $data->online_status == 1 && $data->publish_status == 1) {
            return response()->json(['message' => '该直播已上线发布,不能删除海报', 'status' => false]);
        }

        // 根据 `,` 判断传递过来的是单个id还是多个id
        if (substr_count($id, ',') >= 1) {
            $id = explode(",", $id);
        }

        // 如果是数组则进行批量删除
        if (is_array($id)) {
            if ($flag = $this->vcposterRepository->batchDelete('id', $id)) {
                return response()->json(['message' => __('entity.asset.vlive.vcposters.deleted_success'), 'status' => $flag]);
            } else {
                return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
            }
        }

        $vcposter = $this->vcposterRepository->findWithoutFail($id);

        if (empty($vcposter)) {
            Flash::error(__('entity.asset.vlive.vcposters.not_found'));

            return redirect(route('entity.asset.vlive.vcposters.index'));
        }

        if ($flag = $this->vcposterRepository->delete($id)) {
            DB::table('vchan_vcposters')->where(['vcposter_id' => $id, 'vchan_id' => $vchan_id])->delete();

            return response()->json(['message' => __('entity.asset.vlive.vcposters.deleted_success'), 'status' => $flag]);
        } else {
            return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
        }
    }

    /**
     * [form description]
     * @return {[type]} [description]
     */
    protected function form()
    {
        return Admin::form(Vcposter::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('vcposter_no', '编号');
            $form->text('name', '名称');
            $form->text('title', '标题');
            $form->text('subtitle', '副标题');
            $form->text('short_desc', '简述');
            $form->text('long_desc', '详述');
            $form->text('cposter_url', '海报路径');
            $imgtpls = Imgtpl::pluck('name', 'id');
            $form->select('specification', '海报规格类型')->options($imgtpls);
            $form->text('dimension', '海报尺寸比例');
            $form->number('listorder', '排序');
            $form->datetime('created_at', '创建日期');
            $form->datetime('updated_at', '更新日期');


            // $form->display('created_at', __('base.created_at'));
            // $form->display('updated_at', __('base.updated_at'));

        });
    }

    /**
     * [grid description]
     * @return {[type]} [description]
     */
    protected function grid()
    {

        return Admin::grid(Vcposter::class, function (Grid $grid) {
            // 考虑是否需要scope和排序
            // $grid->model()->orderBy('listorder', 'asc');

            // // 添加按钮
            // if (!\Gate::check('entity.asset.vlive.vcposters.create')) {
            //     $grid->disableCreation();
            // }

            // // 编辑和删除按钮
            // $grid->actions(function ($actions) {
            //     // 编辑按钮
            //     if (!\Gate::check('entity.asset.vlive.vcposters.edit')) {
            //         $actions->disableEdit();
            //     }
            //     // 删除按钮
            //     if (!\Gate::check('entity.asset.vlive.vcposters.destroy')) {
            //         $actions->disableDelete();
            //     }
            // });

            // // 导出按钮
            // if (!\Gate::check('entity.asset.vlive.vcposters.export')) {
            //     $grid->disableExport();
            // }

            // // 批量操作
            // $grid->tools(function ($tools) {
            //     $tools->batch(function ($batch) {
            //         // 批量删除按钮
            //         if (!\Gate::check('entity.asset.vlive.vcposters.batch_destroy')) {
            //             $batch->disableDelete();
            //         }
            //     });
            // });

            // 添加按钮
            if (Admin::user()->cannot('entity.asset.vlive.vcposters.create')) {
                $grid->disableCreation();
            }

            // 编辑和删除按钮
            $grid->actions(function ($actions) {
                // 编辑按钮
                if (Admin::user()->cannot('entity.asset.vlive.vcposters.edit')) {
                    $actions->disableEdit();
                }
                // 删除按钮
                if (Admin::user()->cannot('entity.asset.vlive.vcposters.destroy')) {
                    $actions->disableDelete();
                }
            });

            // 导出按钮
            if (Admin::user()->cannot('entity.asset.vlive.vcposters.export')) {
                $grid->disableExport();
            }

            // 批量操作
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    // 批量删除按钮
                    if (Admin::user()->cannot('entity.asset.vlive.vcposters.batch_destroy')) {
                        $batch->disableDelete();
                    }
                });
            });

            $grid->column('id', 'ID')->sortable();
            $grid->column('vcposter_no', '编号')->sortable();
            $grid->column('name', '名称')->sortable();
            $grid->column('title', '标题')->sortable();
            $grid->column('subtitle', '副标题')->sortable();
            $grid->column('short_desc', '简述')->sortable();
            $grid->column('long_desc', '详述')->sortable();
            // $grid->column('cposter_url', '海报路径')->sortable();
            $grid->column('imgtpl.name', '图像规格')->sortable();
            $grid->column('dimension', '海报尺寸比例')->sortable();
            $grid->column('listorder', '排序')->sortable();
            // $grid->column('created_at', '创建日期')->sortable();
            // $grid->column('updated_at', '更新日期')->sortable();



            /**
             * 过滤处理
             */
            $grid->filter(function ($filter) {
                // // 禁用id查询框
                // $filter->disableIdFilter();
                // 名称
                $filter->like('name', '名称');
                // 标题
                $filter->like('title', '标题');
                // 副标题
                $filter->like('subtitle', '副标题');
                // 图片规格
                $tempImgtpls = Imgtpl::pluck('name', 'id');
                $filter->equal('specification', '图片规格')->select($tempImgtpls);
            });
        });
    }
}
